/*
    Copyright 2010 Northbranchlogic, Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
 */

package com.norbl.runjrun.gui;

import javax.swing.table.*;
import com.norbl.runjrun.*;
import com.norbl.util.*;
import java.util.*;

/**
 *
 * @author Barnet Wagman
 */
public class StateTableModels implements StateChangeListener {

    public DbElTableModel nodeStateTableModel;
    public DbElTableModel taskStateTableModel;
    public NodeSummaryTableModel nodeSummaryTableModel;
    public TaskSummaryTableModel taskSummaryTableModel;

    CurrentStates sm;

    public StateTableModels(CurrentStates sm) {

        this.sm = sm;

        nodeStateTableModel = new DbElTableModel(new NodeState());
        taskStateTableModel = new DbElTableModel(new TaskState());

        nodeSummaryTableModel = new NodeSummaryTableModel(sm.nodeSummary);
        taskSummaryTableModel = new TaskSummaryTableModel(sm.taskSummary);
    }

    public void stateChanged() {
        nodeStateTableModel.updateEls(sm.nodes.currentStates);
        taskStateTableModel.updateEls(sm.tasks.currentStates);
        nodeSummaryTableModel.update();
        taskSummaryTableModel.update();
    }

    class DbElTableModel extends AbstractTableModel {

        DbEl[] els;
        DbEl templateDbEl;
        String[] colNames;

        DbElTableModel(DbEl templateDbEl) {

            this.templateDbEl = templateDbEl;

            DbElField[] flds = templateDbEl.getDisplayFields();
            colNames = new String [flds.length];
            for ( int i = 0; i < flds.length; i++ )
                colNames[i] = flds[i].displayName;

            els = new DbEl[] {};
        }

        public void updateEls(DbEl[] els) {
            this.els = els;
            fireTableDataChanged();
        }

        public int getRowCount() { return(els.length); }
        public int getColumnCount() { return(colNames.length); }
        public String getColumnName(int column) { return(colNames[column]); }

        public Object getValueAt(int rowIndex, int columnIndex) {
            if ( els.length > 0 ) {
                DbEl el = els[rowIndex];
                return( el.getDisplayFields()[columnIndex].getDisplayValue() );
            }
            else return(null);
        }
    }

    class NodeSummaryTableModel extends AbstractTableModel {

        String[] colNames = new String[] {
            "Nodes", "Idle", "Active", "No pulse", "nil"
        };

        CurrentStates.NodeSummary nodeSummary;

        public NodeSummaryTableModel(CurrentStates.NodeSummary nodeSummary) {
            this.nodeSummary = nodeSummary;
        }

        void update() {
            fireTableDataChanged();
        }

        public int getRowCount() { return(1); }
        public int getColumnCount() { return(colNames.length); }
        public String getColumnName(int column) { return(colNames[column]); }

        public Object getValueAt(int rowIndex, int columnIndex) {
            switch(columnIndex) {
                case 0: return( new Integer(nodeSummary.nNodes) );
                case 1: return( new Integer(nodeSummary.nNodesIdle) );
                case 2: return( new Integer(nodeSummary.nNodesActive) );
                case 3: return( new Integer(nodeSummary.nNodesNoPulse) );
                case 4: return( new Integer(nodeSummary.nNodesNil) );
                default: return(null);
            }
        }
    }

    class TaskSummaryTableModel extends AbstractTableModel {

        String[] colNames;

        CurrentStates.TaskSummary taskSummary;

        public TaskSummaryTableModel(CurrentStates.TaskSummary taskSummary) {
            this.taskSummary = taskSummary;

            colNames = new String[] {
                "Tasks", "Waiting", "Running", "Finished",
                "Exception terminated",
                "Missing Runable",  "x"};

            if ( taskSummary.autoRequeueOrphans )
                colNames[6] = "Re-queued";
            else colNames[6] = "Orphans";
        }

        void update() {
            fireTableDataChanged();
        }

        public int getRowCount() { return(1); }
        public int getColumnCount() { return(colNames.length); }
        public String getColumnName(int column) { return(colNames[column]); }

        public Object getValueAt(int rowIndex, int columnIndex) {
            switch(columnIndex) {
                case 0: return( new Integer(taskSummary.nTasks) );
                case 1: return( new Integer(taskSummary.nTasksWaiting) );
                case 2: return( new Integer(taskSummary.nTasksRunning) );
                case 3: return( new Integer(taskSummary.nTasksFinished) );
                case 4: return( new Integer(taskSummary.nTasksTerminatedByException) );
                case 5: return( new Integer(taskSummary.nTasksMissingRunnable));
                case 6:
                    if ( taskSummary.autoRequeueOrphans )
                        return( new Integer(taskSummary.nRequeued) );
                    else return( new Integer(taskSummary.nTasksOrphaned) );
                default: return(null);
            }
        }
    }
}
